{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "655b85d8",
   "metadata": {},
   "source": [
    "# pandas数据结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9ebe82b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a5e8ee5",
   "metadata": {},
   "source": [
    "## Series"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8937cb99",
   "metadata": {},
   "source": [
    "Series\n",
    "l = np.array([1,2,3,6,9])\n",
    "\n",
    "s1 = pd.Series(data = l) #Series是一维的数组，和NumPy数组不一样：Series多了索引\n",
    "display(l,s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5dfca9ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    1\n",
       "B    2\n",
       "C    3\n",
       "D    6\n",
       "E    9\n",
       "dtype: int32"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2 = pd.Series(data = l,index = list('ABCDE'))\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5e5fb9d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    149\n",
       "B    130\n",
       "C    118\n",
       "D     99\n",
       "E     66\n",
       "dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s3 = pd.Series(data={'A':149,'B':130,'C':118,'D':99,'E':66})\n",
    "s3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "920e21f8",
   "metadata": {},
   "source": [
    "## DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b37a2427",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Math</th>\n",
       "      <th>En</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>29</td>\n",
       "      <td>44</td>\n",
       "      <td>137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>124</td>\n",
       "      <td>148</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>80</td>\n",
       "      <td>103</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>59</td>\n",
       "      <td>140</td>\n",
       "      <td>132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>124</td>\n",
       "      <td>120</td>\n",
       "      <td>108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>73</td>\n",
       "      <td>22</td>\n",
       "      <td>132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>119</td>\n",
       "      <td>77</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>97</td>\n",
       "      <td>24</td>\n",
       "      <td>135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>110</td>\n",
       "      <td>10</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>K</th>\n",
       "      <td>15</td>\n",
       "      <td>125</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Math   En\n",
       "A      29    44  137\n",
       "B     124   148   19\n",
       "C      80   103   25\n",
       "D      59   140  132\n",
       "E     124   120  108\n",
       "F      73    22  132\n",
       "H     119    77   21\n",
       "I      97    24  135\n",
       "J     110    10   64\n",
       "K      15   125   18"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series是一维的，功能比较少\n",
    "#DataFrame是二维的，多个Series共用索引，组成了DataFrame\n",
    "df1 = pd.DataFrame(data = np.random.randint(0,151,size=(10,3)),\n",
    "                   index = list('ABCDEFHIJK'), #行索引\n",
    "                   columns=['Python','Math','En']) #列索引\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed7ae5b1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e153f3f7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f926bbf",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
